import 'package:flutter/material.dart';

class CommonHeader extends StatefulWidget implements PreferredSizeWidget {
  final String title;
  final Color? bgColor;
  final Color? titleColor;
  final bool? showBackIcon;

  const CommonHeader(
      {super.key,
      required this.title,
      this.bgColor,
      this.titleColor,
      this.showBackIcon});

  @override
  State<CommonHeader> createState() => _CommonHeaderState();

  @override
  // TODO: implement preferredSize
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}

class _CommonHeaderState extends State<CommonHeader> {
  Widget titleWidget() {
    Widget content = Text(widget.title,
        style: TextStyle(color: widget.titleColor ?? Colors.black));
    return content;
  }

  Widget backIcon() {
    Widget content = IconButton(
      icon: const Icon(Icons.arrow_back_ios),
      onPressed: () {
        Navigator.pop(context);
      },
    );
    return content;
  }

  @override
  Widget build(BuildContext context) {
    Widget header = AppBar(
        title: titleWidget(),
        centerTitle: true,
        leading: widget.showBackIcon == true ? backIcon() : null,
        backgroundColor: widget.bgColor ?? Colors.blueGrey
    );
    header = PreferredSize(preferredSize: const Size.fromHeight(50), child: header);
    return header;
  }
}
